﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using CN.DBML;

namespace CityNavigation
{
    /// <summary>
    /// Summary description for AutoCompleteHandler
    /// </summary>
    public class AutoCompleteHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/xml";     //设置输出的格式为XML
            context.Response.Charset = "UTF-8";              //设置输出的编码为UTF-8
            //这是用来获取下拉列表框中下面提示的项目数据，获取的数据用XML格式输出
            CityNavigationDataContext dc = new CityNavigationDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["CityNavigationConnectionString"].ConnectionString);
            StringBuilder xmlData = new StringBuilder();
            xmlData.AppendLine(@"<?xml version='1.0' encoding='UTF-8' ?>");
            xmlData.AppendLine(@"<response>");
            string names = context.Request.QueryString["names"];
            string isStation = context.Request.QueryString["isStation"];

            if (bool.Parse(isStation))
            {
                int i = 0;//统计数量
                var data = (from item in dc.BusPassStations
                            where item.StationName.StartsWith(names)
                            select item.StationName).Distinct().ToList();
                foreach (var item in data)
                {
                    if (i > 20) break;
                    xmlData.AppendLine(@"<res>" + item.ToString() + "</res>");
                    i++;
                }
                if (i < 20)
                {
                    var data1 = (from item in dc.BusPassStations
                                 where item.StationName.Contains(names) && !item.StationName.StartsWith(names)
                                 select item.StationName).Distinct().ToList();
                    foreach (var item in data1)
                    {
                        if (i > 20) break;
                        xmlData.AppendLine(@"<res>" + item.ToString() + "</res>");
                        i++;
                    }
                }
                xmlData.AppendLine(@"</response>");
                context.Response.Write(xmlData.ToString());
            }
            else
            {
                int i = 0;//统计数量
                var data = (from item in dc.BusLines
                            where item.LineName.StartsWith(names)
                            orderby item.LineName.Length 
                            select item.LineName).Distinct().ToList();
                foreach (var item in data)
                {
                    if (i > 20) break;
                    xmlData.AppendLine(@"<res>" + item.ToString() + "</res>");
                    i++;
                }
                if (i < 20)
                {
                    var data1 = (from item in dc.BusLines
                                 where item.LineName.Contains(names) && !item.LineName.StartsWith(names)
                                 orderby item.LineName.Length 
                                 select item.LineName).Distinct().ToList();
                    foreach (var item in data1)
                    {
                        if (i > 20) break;
                        xmlData.AppendLine(@"<res>" + item.ToString() + "</res>");
                        i++;
                    }
                }
                xmlData.AppendLine(@"</response>");
                context.Response.Write(xmlData.ToString());
            }
        }

        private void GetXmlData()
        {   
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}